// pages/test_detail/test_detail.js
const app = getApp()
const login_js = require('../../utils/login.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    detail: {},
    challenge_list: [],
    test_list: [],
    left_list: [],
    right_list: [],
    model: ['+', '-', '×', '÷'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id
    this.setData({
      id
    })
    this.getDetail()
    this.getChallenge()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '这题我会做',
      path: `/pages/test_detail/test_detail?id=${this.data.id}`
    }
  },
  /* 
  朋友圈
  */
  onShareTimeline() {
    return {
      query: `id=${this.data.id}`
    }
  },
  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      modal_show: false,
    })
    wx.redirectTo({
      url: `/pages/challenge/challenge?id=${this.data.id}`,
    })
  },
  /* 获取题目 */
  getDetail() {
    wx.showLoading({
      title: '题目加载中',
    })
    wx.cloud.callFunction({
      name: 'runDb',
      data: {
        type: 'get_detail',
        collection: 'test',
        is_where: {
          "_id": this.data.id
        },
      },
      complete: res => {
        let detail = res.result.data[0]
        // console.log(detail)
        let list = detail.test
        list.forEach(i => {
          i.math_model = this.data.model[i.model_idx]
        })
        wx.hideLoading({})
        // console.log(list)
        this.setData({
          detail,
          test_list: list
        })
        this.renderList(this.data.test_list)
      }
    })
  },
  /**
   * 根据list生成左右list
   * @params {array} list
   */
  renderList(list = []) {
    let left_list = this.data.left_list
    let right_list = this.data.right_list
    list.forEach((item, idx) => {
      if (idx % 2) {
        right_list.push(item)
      } else {
        left_list.push(item)
      }
    })
    this.setData({
      left_list,
      right_list,
    })
  },
  /* 获取挑战者 */
  getChallenge() {
    wx.cloud.callFunction({
      name: 'runDb',
      data: {
        type: 'get_list',
        collection: 'challenge',
        is_where: {
          "test_id": this.data.id
        },
        sort: 'score',
        sort_category: 'desc',
        skip: 0,
        limit: 20
      },
      complete: res => {
        let list = res.result.data
        // console.log(list)
        this.setData({
          challenge_list: list
        })
      }
    })
  },
  /* 点击挑战者 */
  clickRank() {
    if (this.data.is_rank) {
      this.setData({
        is_rank: false
      })
    } else {
      this.setData({
        is_rank: true
      })
    }
  },
  /* 点击挑战 */
  clickChallenge() {
    // 登录
    // 如果未登录则弹框
    if (!app.global_data.user_data.openid) {
      this.setData({
        modal_show: true
      })
      return
    }
    wx.redirectTo({
      url: `/pages/challenge/challenge?id=${this.data.id}`,
    })
  }
})